package com.geek.aop;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

import java.nio.charset.Charset;

@Configuration
@ComponentScan("com.geek.aop")
@PropertySource("classpath:test.properties")
@Slf4j
public class ELConfig {
    @Value("你说你的心不再温热如昔")
    private String normal;

    @Value("#{systemProperties['os.name']}")
    private String osName;

    @Value("#{ T(Math).random()*100.00 }")
    private double randomNumber;

    @Value("#{demoService.annother}")
    private String fromAnnother;

    @Value("classpath:test.txt")
    private Resource testFile;

    @Value("http://www.baidu.com")
    private Resource testUrl;

    @Value("${book.name}")
    private String bookName;

    @Autowired
    private Environment environment;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    public void outputResource() {
        try {
            log.info(normal);
            log.info(osName);
            log.info(String.valueOf(randomNumber));
            log.info(fromAnnother);
            log.info(IOUtils.toString(testFile.getInputStream(), Charset.defaultCharset()));
            log.info(IOUtils.toString(testUrl.getInputStream(), Charset.defaultCharset()));
            log.info(bookName);
            log.info(environment.getProperty("book.author"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
